/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.sys.service.impl;

import com.arisgo.cloud.auth.sys.entity.Column;
import com.arisgo.cloud.auth.sys.entity.Tabulation;
import com.arisgo.cloud.auth.sys.mapper.ColumnMapper;
import com.arisgo.cloud.auth.sys.mapper.TableMapper;
import com.arisgo.cloud.auth.sys.service.TableService;
import com.arisgo.cloud.auth.sys.vo.ColumnVo;
import com.arisgo.cloud.auth.sys.vo.TableVo;
import com.arisgo.cloud.web.mybatis.service.CloudServiceImpl;
import com.arisgo.cloud.web.param.Param;
import com.arisgo.cloud.web.utils.TreeUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表
 *
 * @author Arisgo
 * @since 2024-07-02
 */
@Service
@AllArgsConstructor
public class TableServiceImpl extends CloudServiceImpl<TableMapper, Tabulation, TableVo> implements TableService {

    @Override
    public Tabulation getInstanceModel() {
        return new Tabulation();
    }

    @Override
    public TableVo getInstanceVo() {
        return new TableVo();
    }

    private final ColumnMapper columnMapper;

    @Override
    public TableVo findByCode(String username, String code) {
        TableVo table = findOneByParams(Param.creator(Tabulation.CODE, code).buildWrapper());
        List<ColumnVo> vos = columnMapper.findByTableId(username, table.getId());
        table.setColumns(TreeUtil.convertTree(vos));
        return table;
    }
}
